//
//  YZCircleAnimator.m
//  donghua
//
//  Created by yongzhen on 16/10/24.
//  Copyright © 2016年 yongzhen. All rights reserved.
//

#import "YZCircleAnimator.h"

@interface YZCircleAnimator() <UIViewControllerAnimatedTransitioning,CAAnimationDelegate>

@end


@implementation YZCircleAnimator
{
    BOOL _isPresent;
   __weak id <UIViewControllerContextTransitioning> _transitionContext;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
{
    _isPresent = YES;
    return  self;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
{
    _isPresent = NO;
    return  self;
}
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;
{
   
    return 0.25;
    
    
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
{
    
    // 获取容器视图
    UIView *contentView  = [transitionContext containerView];
    
    
    // 获取目标视图
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *view = _isPresent ? toView : fromView;
    
    //目标视图添加到容器视图
    if (_isPresent) {
        
        [contentView addSubview:view];
    }
    //执行动画
    [self circleAnimWithView:view];
    _transitionContext = transitionContext;
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
{
    
    NSLog(@"已完成");
    // 完成
    [_transitionContext completeTransition:YES];
    
    
}

-(void)circleAnimWithView:(UIView *)view
{
    
    // 1，实例化图层
    CAShapeLayer *layer = [CAShapeLayer layer];
    // 2,设置图层属性
    // 路径
    CGFloat radius = 25;
    CGFloat margin = 20;
    CGFloat viewWidth = view.bounds.size.width;
    CGFloat viewHeight = view.bounds.size.height;
    // 初始位置
    CGRect rect = CGRectMake(viewWidth - 2 * radius - margin,  margin ,2 * radius, 2 * radius);
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithOvalInRect:rect];
    layer.path = beginPath.CGPath;
    // 结束位置
    // 结束位置 - 利用缩进，参数为负，是放大矩形，中心点保持不变
    
    CGFloat maxRadius = sqrt( viewWidth * viewWidth + viewHeight * viewHeight);
    CGRect endRect = CGRectInset(rect, -maxRadius, -maxRadius);
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    
    
    // 提示：一旦设置为 mask 属性，填充颜色无效！
    // layer.fillColor = [UIColor redColor].CGColor;
    
    [view.layer addSublayer:layer];
    
    view.layer.mask = layer;
    //  动画 - 如果要做 shapeLayer 的动画，不能使用 UIView 的动画方法，应该用核心动画
    // 实例化动画对象
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];
    //设置动画属性
    anim.duration = [self transitionDuration:_transitionContext];
    if (_isPresent) {
        
        anim.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
        anim.toValue = (__bridge id _Nullable)(endPath.CGPath);
    }else{
        anim.toValue = (__bridge id _Nullable)(beginPath.CGPath);
        anim.fromValue = (__bridge id _Nullable)(endPath.CGPath);
    }
    // 设置向前填充模式
    anim.fillMode = kCAFillModeForwards;
    // 完成之后不删除
    anim.removedOnCompletion = NO;
    anim.delegate = self;
    // 3> 将动画添加到图层 - ShaperLayer，让哪个图层动画，就应该将动画添加到哪个图层
    [layer addAnimation:anim forKey:nil];

    
    
    
}



@end
